package hu.diploma.web.client.pages;

import hu.diploma.web.client.Messages;
import hu.diploma.web.client.service.WebCallService;
import hu.diploma.web.client.service.WebCallServiceAsync;
import hu.diploma.web.shared.webvos.CallWebVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.charts.client.Chart;
import com.extjs.gxt.charts.client.event.ChartEvent;
import com.extjs.gxt.charts.client.event.ChartListener;
import com.extjs.gxt.charts.client.model.ChartModel;
import com.extjs.gxt.charts.client.model.Legend;
import com.extjs.gxt.charts.client.model.Legend.Position;
import com.extjs.gxt.charts.client.model.charts.PieChart;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.MarginData;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class StatsPage extends LayoutContainer {

	private static StatsPage instanse;
	private final Messages messages = GWT.create(Messages.class);
	WebCallServiceAsync personService = GWT.create(WebCallService.class);

	public static StatsPage getWidget() {

		if (instanse == null) {
			instanse = new StatsPage();
		}
		return instanse;
	}

	private final ChartListener listener = new ChartListener() {

		@Override
		public void chartClick(ChartEvent ce) {
			Info.display("Chart Clicked", "You selected {0}.", "" + ce.getValue());
		}
	};

	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		ContentPanel cp = new ContentPanel();
		cp.setHeading(messages.chart_device());
		cp.setFrame(true);
		cp.setSize(500, 500);
		cp.setLayout(new FitLayout());

		String url = "";

		final Chart chart = new Chart("gxt/chart/open-flash-chart.swf");
		chart.setBorders(true);
		chart.setChartModel(getPieChartData());

		cp.add(chart);

		add(cp, new MarginData(20));
	}

	private ChartModel getPieChartData() {
		final ChartModel cm = new ChartModel(messages.chart_device_title());
		cm.setBackgroundColour("#fffff5");
		Legend legend = new Legend(Position.RIGHT, true);
		legend.setPadding(10);
		cm.setLegend(legend);

		final PieChart pieChart = new PieChart();
		pieChart.setAlpha(0.5f);
		pieChart.setNoLabels(true);
		pieChart.setTooltip("#label# #val#db<br>#percent#");

		//
	
		// pie.addSlices(new PieChart.Slice(150, "JP", "Japan"));
		// pie.addSlices(new PieChart.Slice(120, "DE", "Germany"));
		// pie.addSlices(new PieChart.Slice(60, "UK", "United Kingdom"));
		// pie.addChartListener(listener);

		personService.loadAll(new AsyncCallback<List<CallWebVo>>() {

			public void onSuccess(List<CallWebVo> result) {
				Map<String, Integer> map = new HashMap<String, Integer>();

				for (CallWebVo callWebVo : result) {

					String name = callWebVo.getNumber();
					if (!map.containsKey(name)) {
						map.put(name, 1);
					} else {
						Integer integer = map.get(name);
						integer++;
						map.put(name, integer);
					}
				}

				ArrayList<String> colours = new ArrayList<String>();
				for (String name : map.keySet()) {

					if (name == null) {
						String dname = "Ismeretlen";
						pieChart.addSlices(new PieChart.Slice(map.get(name), dname, dname));
					} else {
						pieChart.addSlices(new PieChart.Slice(map.get(name), name, name));
					}

					colours.add(getRandomColor());
				}

				pieChart.setColours("#ff0000", "#00aa00", "#0000ff", "#ff9900", "#ff00ff", "#99aaff");
				cm.addChartConfig(pieChart);
				doLayout();
			}

			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		});

		return cm;
	}

	public static String getRandomColor() {

		String hex1 = getRandomHex();
		String hex2 = getRandomHex();
		String hex3 = getRandomHex();
		String hex4 = getRandomHex();
		String hex5 = getRandomHex();
		String hex6 = getRandomHex();

		String color = "#" + hex1 + hex2 + hex3 + hex4 + hex5 + hex6;

		return color;
	}

	/**
	 * get random hex
	 * 
	 * @return int
	 */
	private static String getRandomHex() {
		String[] hex = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
		int randomNum = Random.nextInt(hex.length);
		String sHex = hex[randomNum];
		return sHex;
	}

}
